<?php

namespace App\Http\Controllers\Admin;

/**
 * 礼物墙评分奖励管理
 */

use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;                //引用http请求类
use App\Exceptions\BasicException;          //引入框架异常处理类
use App\Response\Response;

class GiftWallController extends BaseController
{
    /**
     * @api {get} /giftWall/exchangeConfigDetail 礼物兑换配置详情
     * @apiVersion 0.1.0
     * @apiName 礼物兑换配置详情
     * @apiGroup 礼物墙管理
     *
     * @apiParam {int} id   配置ID 非必传（当不传时，返回基础数据列表）
     *
     * @apiError 10010 参数校验错误
     * @apiError 70101 没有符合条件的记录
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": {
    "all_gift_list 礼物列表": [{
    "id": 379,
    "name 名称": "\u5468\u661f*\u91d1\u51a0",
    "picture": "http:\/\/yutangyuyin.oss-cn-hangzhou.aliyuncs.com\/admin_images\/5fea93d5d9bde.png",
    "price": 999999999,
    "special": "",
    "type": 15,
    "sold 礼物状态 1：上架中，2：已下架": 1,
    "cardiac": 0,
    "frame_id 外框ID": 0
    }, {
    "id": 380,
    "name": "\u5468\u661f*\u94f6\u51a0",
    "picture": "http:\/\/yutangyuyin.oss-cn-hangzhou.aliyuncs.com\/admin_images\/5fea93dedac81.png",
    "price": 888888888,
    "special": "",
    "type": 16,
    "sold": 1,
    "cardiac": 0,
    "frame_id": 0
    }],
    "detail 兑换详情": {
    "id": 4,
    "title": "礼物兑换1",
    "content": [{
    "gift_id": 437,
    "target_star 目标星数": 0
    }, {
    "gift_id": 347,
    "target_star": 2
    }, {
    "gift_id": 325,
    "target_star": 1
    }],
    "status 状态 0：不展示，1：展示，2：已删除": 1,
    "reward_type 奖励类型 1：道具包": 1,
    "reward_id 道具包ID": 115,
    "reward_number 道具包数量": 1,
    "add_time": "2021-01-25 19:21:57",
    "update_time": "2021-01-25 19:32:27"
    },
    "reward_list 道具包列表": [{
    "id": 135,
    "name": "海洋之心"
    }, {
    "id": 134,
    "name": "帽子戏法"
    }]
    }
    }
     *
     */
    public function exchangeConfigDetail(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'id'    => 'numeric|gt:0'
            ]
        );

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $id = $request->input('id', 0);
        $result = app('repo_gift_wall')->exchangeConfigDetail($id);

        return Response::sendData($result);
    }

    /**
     * @api {post} /giftWall/exchangeConfigSave 礼物兑换配置保存
     * @apiVersion 0.1.0
     * @apiName 礼物兑换配置保存
     * @apiGroup 礼物墙管理
     *
     * @apiParam {number} id   记录ID，非必传（不传为新增，传则为编辑）
     * @apiParam {string} title    标题，非必传
     * @apiParam {string} content    配置内容（json格式[{"gift_id":437,"target_star":0},{"gift_id":347,"target_star":2},{"gift_id":325,"target_star":1}]），必传
     * @apiParam {number} reward_id     道具包ID，必传
     * @apiParam {number} reward_number     道具包数量，必传
     * @apiParam {number} status     状态，必传（0：不展示，1：展示）
     *
     * @apiError 10010 参数校验错误
     * @apiError 70101 没有符合条件的记录
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": []
    }
     *
     */
    public function exchangeConfigSave(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'numeric|gt:0',
                'title' => 'string|max:20',
                'content' => 'required|string|max:200',
                'reward_id' => 'required|numeric|gt:0',
                'reward_number' => 'required|numeric|gt:0',
                'status' => 'required|numeric|in:0,1',
            ]
        );

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $param = [
            'id' => $request->input('id', 0),
            'title' => $request->input('title', ''),
            'content' => $request->input('content'),
            'reward_id' => $request->input('reward_id', 0),
            'reward_number' => $request->input('reward_number', 0),
            'status' => $request->input('status')
        ];
        $result = app('repo_gift_wall')->exchangeConfigSave($param);

        return Response::sendData($result);
    }

    /**
     * @api {post} /giftWall/exchangeConfigDelete 礼物兑换配置删除
     * @apiVersion 0.1.0
     * @apiName 礼物兑换配置删除
     * @apiGroup 礼物墙管理
     *
     * @apiParam {string} ids   记录ID，必传（多个则以英文逗号隔开）
     *
     * @apiError 10010 参数校验错误
     * @apiError 70102 符合删除记录数量不足
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": []
    }
     *
     */
    public function exchangeConfigDelete(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'ids' => 'required'
            ]
        );

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $ids = $request->input('ids');
        $ids = explode(',', $ids);
        $result = app('repo_gift_wall')->exchangeConfigDelete($ids);

        return Response::sendData($result);
    }

    /**
     * @api {get} /giftWall/exchangeConfigList 礼物兑换配置列表
     * @apiVersion 0.1.0
     * @apiName 礼物兑换配置列表
     * @apiGroup 礼物墙管理
     *
     * @apiParam {int} page 请求页码  非必传（默认1）
     * @apiParam {int} per_page 分页尺寸  非必传（默认20）
     *
     * @apiError 10010 参数校验错误
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": {
    "total 总条数": 1,
    "current_page 当前页码数": 1,
    "last_page 最后一页码数": 1,
    "stime 兑换开始时间": "2021-01-15",
    "etime 兑换结束时间": "2021-05-15",
    "list": [{
    "id": 9,
    "title": "礼物兑换6",
    "content": [{
    "gift_id": 434,
    "target_star": 0,
    "gift_name": "气泡超火",
    "gift_picture": "http:\/\/yutangyuyin1.oss-cn-hangzhou.aliyuncs.com\/admin_images\/600e649757d93.png",
    "gift_price": 1
    }, {
    "gift_id": 435,
    "target_star": 2,
    "gift_name": "气泡飞机",
    "gift_picture": "http:\/\/yutangyuyin1.oss-cn-hangzhou.aliyuncs.com\/admin_images\/600e64b4b0474.png",
    "gift_price": 1
    }, {
    "gift_id": 351,
    "target_star": 1,
    "gift_name": "梦幻城堡",
    "gift_picture": "http:\/\/yutangyuyin.oss-cn-hangzhou.aliyuncs.com\/admin_images\/5fae3e255983a.png",
    "gift_price": 334400
    }],
    "status": 1,
    "reward_type": 1,
    "reward_id": 120,
    "reward_number": 1,
    "add_time": "2021-01-25 19:22:09",
    "update_time": "2021-01-25 19:32:27",
    "reward_picture": "http:\/\/yutangyuyin1.oss-cn-hangzhou.aliyuncs.com\/newadmin\/d551b699316307fd776f50fa41e10c14Gqdr7.png",
    "reward_name": ""
    }, {
    "id": 8,
    "title": "礼物兑换5",
    "content": [{
    "gift_id": 436,
    "target_star": 0,
    "gift_name": "气泡火箭",
    "gift_picture": "http:\/\/yutangyuyin1.oss-cn-hangzhou.aliyuncs.com\/admin_images\/600e64cbbf052.png",
    "gift_price": 1
    }, {
    "gift_id": 433,
    "target_star": 2,
    "gift_name": "气泡宝宝",
    "gift_picture": "http:\/\/yutangyuyin1.oss-cn-hangzhou.aliyuncs.com\/admin_images\/600e5bdfaad30.png",
    "gift_price": 1
    }, {
    "gift_id": 352,
    "target_star": 1,
    "gift_name": "灿若烟花",
    "gift_picture": "http:\/\/yutangyuyin.oss-cn-hangzhou.aliyuncs.com\/admin_images\/5fae4696f02fd.png",
    "gift_price": 131400
    }],
    "status": 1,
    "reward_type": 1,
    "reward_id": 119,
    "reward_number": 1,
    "add_time": "2021-01-25 19:22:07",
    "update_time": "2021-01-25 19:32:27",
    "reward_picture": "http:\/\/yutangyuyin1.oss-cn-hangzhou.aliyuncs.com\/newadmin\/475b5643b040d4fac5a28461423506efT3uY7.png",
    "reward_name": ""
    }]
    }
    }
     *
     */
    public function exchangeConfigList(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'page' => 'numeric|gt:0',
                'per_page' => 'numeric|gt:0',
            ]
        );

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $page = $request->input('page', 1);
        $perPage = $request->input('per_page', 20);
        $result = app('repo_gift_wall')->exchangeConfigList($page, $perPage);

        return Response::sendData($result);
    }

    /**
     * @api {post} /giftWall/exchangeDateSet 礼物兑换日期设置
     * @apiVersion 0.1.0
     * @apiName 礼物兑换日期设置
     * @apiGroup 礼物墙管理
     *
     * @apiParam {string}  stime  开始时间（例：2021-01-15）
     * @apiParam {string}  etime  结束时间（例：2021-01-16）
     *
     * @apiError 10010 参数校验错误
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": []
    }
     *
     */
    public function exchangeDateSet(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'stime' => 'required|date_format:"Y-m-d"',
                'etime' => 'required|date_format:"Y-m-d"|after:stime'
            ]
        );

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        app('redis')->set('gift_wall:exchange_stime', $request->input('stime'));
        app('redis')->set('gift_wall:exchange_etime', $request->input('etime'));

        return Response::sendData([]);
    }

    /**
     * @api {get} /giftWall/scorePrizeConfigDetail 礼物墙评分奖励配置详情
     * @apiVersion 0.1.0
     * @apiName 礼物墙评分奖励配置详情
     * @apiGroup 礼物墙管理
     *
     * @apiParam {int} id   配置ID 非必传（当不传时，返回基础数据列表）
     *
     * @apiError 10010 参数校验错误
     * @apiError 70101 没有符合条件的记录
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": {
    "detail 记录详情": {
    "id": 9,
    "title": "评分奖励1",
    "target_score": 20,
    "extra_type": 0,
    "extra_score": 0,
    "color": 1,
    "reward_type": 1,
    "reward_id": 70,
    "reward_number": 1,
    "status": 1,
    "add_time": null,
    "update_time": null
    },
    "gift_type_list 类型列表": [{
    "id": 23,
    "name": "普通类"
    }, {
    "id": 24,
    "name": "特殊类"
    }, {
    "id": 25,
    "name": "活动类"
    }, {
    "id": 27,
    "name": "拍卖房"
    }],
    "color_list 颜色列表": [{
    "id": 1,
    "name": "蓝色"
    }, {
    "id": 2,
    "name": "紫色"
    }, {
    "id": 3,
    "name": "粉红色"
    }, {
    "id": 4,
    "name": "绿色"
    }, {
    "id": 5,
    "name": "橙色"
    }],
    "reward_list 礼包列表": [{
    "id": 1,
    "name": "10积分"
    }, {
    "id": 135,
    "name": "海洋之心"
    }],
    "status_list": [{
    "id": 0,
    "name": "不展示"
    }, {
    "id": 1,
    "name": "展示"
    }]
    }
    }
     *
     */
    public function scorePrizeConfigDetail(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'id'    => 'numeric|gt:0'
            ]
        );

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $id = $request->input('id', 0);
        $result = app('repo_gift_wall')->scorePrizeConfigDetail($id);

        return Response::sendData($result);
    }

    /**
     * @api {post} /giftWall/scorePrizeConfigSave 礼物墙评分奖励配置保存
     * @apiVersion 0.1.0
     * @apiName 礼物墙评分奖励配置保存
     * @apiGroup 礼物墙管理
     *
     * @apiParam {number} id   记录ID，非必传（不传为新增，传则为编辑）
     * @apiParam {string} title    标题，非必传
     * @apiParam {number} target_score    评分值，必传
     * @apiParam {number} extra_type     额外评分类型ID，非必传
     * @apiParam {number} extra_score     额外评分值，非必传
     * @apiParam {number} color     背景图ID，必传
     * @apiParam {number} reward_id     道具包ID，必传
     * @apiParam {number} reward_number     道具包数量，非必传（默认1）
     * @apiParam {number} status     状态，必传
     *
     * @apiError 10010 参数校验错误
     * @apiError 70101 没有符合条件的记录
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": []
    }
     *
     */
    public function scorePrizeConfigSave(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'numeric|gt:0',
                'title' => 'string|max:20',
                'target_score' => 'required|numeric|gt:0',
                'extra_type' => 'numeric|gt:0',
                'extra_score' => 'numeric|gt:0',
                'color' => 'required|numeric|gt:0',
                'reward_id' => 'required|numeric|gt:0',
                'reward_number' => 'numeric|gt:0',
                'status' => 'required|numeric|in:0,1',
            ]
        );

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $param = [
            'id' => $request->input('id', 0),
            'title' => $request->input('title', ''),
            'target_score' => $request->input('target_score'),
            'extra_type' => $request->input('extra_type', 0),
            'extra_score' => $request->input('extra_score', 0),
            'color' => $request->input('color'),
            'reward_id' => $request->input('reward_id'),
            'reward_number' => $request->input('reward_number', 1),
            'status' => $request->input('status')
        ];
        $result = app('repo_gift_wall')->scorePrizeConfigSave($param);

        return Response::sendData($result);
    }

    /**
     * @api {post} /giftWall/scorePrizeConfigDelete 礼物墙评分奖励配置删除
     * @apiVersion 0.1.0
     * @apiName 礼物墙评分奖励配置删除
     * @apiGroup 礼物墙管理
     *
     * @apiParam {string} ids   记录ID，必传（多个则以英文逗号隔开）
     *
     * @apiError 10010 参数校验错误
     * @apiError 70102 符合删除记录数量不足
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": []
    }
     *
     */
    public function scorePrizeConfigDelete(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'ids' => 'required'
            ]
        );

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $ids = $request->input('ids');
        $ids = explode(',', $ids);
        $result = app('repo_gift_wall')->scorePrizeConfigDelete($ids);

        return Response::sendData($result);
    }

    /**
     * @api {get} /giftWall/scorePrizeConfigList 礼物墙评分奖励配置列表
     * @apiVersion 0.1.0
     * @apiName 礼物墙评分奖励配置列表
     * @apiGroup 礼物墙管理
     *
     * @apiParam {int} page 请求页码  非必传（默认1）
     * @apiParam {int} per_page 分页尺寸  非必传（默认20）
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": {
    "total":43,
    "current_page 当前页码数": 1,
    "last_page 最后一页页码数": 3,
    "list": [{
    "id": 9,
    "title": "评分奖励1",
    "target_score": 20,
    "extra_type 额外条件类型ID": 23,
    "extra_score": 10,
    "color 背景图颜色": "蓝色",
    "reward_type": 1,
    "reward_id 道具包ID": 70,
    "reward_number 道具包数量": 1,
    "status 状态 0：不展示，1：展示": 1,
    "add_time": null,
    "update_time": "2021-02-19 11:21:48",
    "reward_picture 道具包封面图": "http:\/\/yutangyuyin1.oss-cn-hangzhou.aliyuncs.com\/newadmin\/0974e7723df693206f69270ed51472d2eY856.png",
    "reward_name": "",
    "extra_type_name 额外条件名称": "普通类"
    }, {
    "id": 10,
    "title": "评分奖励2",
    "target_score": 30,
    "extra_type": 0,
    "extra_score": 0,
    "color": "蓝色",
    "reward_type": 1,
    "reward_id": 72,
    "reward_number": 1,
    "status": 1,
    "add_time": null,
    "update_time": "2021-02-18 18:26:17",
    "reward_picture": "http:\/\/yutangyuyin1.oss-cn-hangzhou.aliyuncs.com\/newadmin\/eab027316f37a91c42d67ab977829a45ayB5f.png",
    "reward_name": "",
    "extra_type_name": ""
    }]
    }
    }
     *
     */
    public function scorePrizeConfigList(Request $request)
    {
        $v = app('validator')->make(
            $request->all(),
            [
                'page' => 'numeric|gt:0',
                'per_page' => 'numeric|gt:0',
            ]
        );

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $page = $request->input('page', 1);
        $perPage = $request->input('per_page', 20);
        $result = app('repo_gift_wall')->scorePrizeConfigList($page, $perPage);

        return Response::sendData($result);
    }
}
